<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of owners_controller
 *
 * @author wilson
 */
class Ownerscontroller extends AppController {

    //put your code here
    var $name = "Owners";

    function index($property_id) {
        $this->set('property_id', $property_id);

        $this->set('owners', $this->Owner->find('all', array('conditions' => array('Owner.property_id' => $property_id), 'fields' => array('Owner.nombres', 'Owner.id', 'Owner.primer_apellido', 'Owner.segundo_apellido', 'Property.nombre', 'Owner.documento'))));
    }
    function evaluation_index($property_id) {
        $this->set('property_id', $property_id);

        $this->set('owners', $this->Owner->find('all', array('conditions' => array('Owner.property_id' => $property_id), 'fields' => array('Owner.nombres', 'Owner.id', 'Owner.primer_apellido', 'Owner.segundo_apellido', 'Property.nombre', 'Owner.documento'))));
    }

    function add($predio_id) {
        APP::Import('model', 'City');
        $City = new City();
        $City->recursive = -1;
        $this->set('departaments', $City->Departament->find('list', array('fields' => array('Departament.id', 'Departament.nombre'))));
        if (!empty($this->data)) {
            $this->Owner->create();
            if ($this->Owner->saveAll($this->data)) {
                $this->Session->setFlash('Propietario Adicionado con éxito');
                $this->setAction('index', $predio_id);
            }
        } else {

            $this->request->data['Owner']['property_id'] = $predio_id;
        }
    }

    function edit($id) {
        APP::Import('model', 'City');
        $City = new City();
        $City->recursive = -1;
        $this->set('departaments', $City->Departament->find('list', array('fields' => array('Departament.id', 'Departament.nombre'))));
        $this->set('cities', $City->find('list', array(
                    'order' => 'nombre ASC',
                    'fields' => array('City.id', 'City.nombre')
                        )
                ));
        if (!empty($this->data)) {

            if ($this->Owner->saveAll($this->data)) {
                $this->Session->setFlash('Propietario Editado con éxito');
                $this->setAction('index', $this->data['Owner']['property_id']);
            }
        } else {

            $this->data = $this->Owner->find("first", array('recursive' => -1, "conditions" => array("Owner.id" => $id), 'joins' => array(array('table' => 'cities', 'alias' => 'City', 'type' => 'left', 'conditions' => 'City.id=Owner.city_id'), array('table' => 'departaments', 'alias' => 'Departament', 'type' => 'left', 'conditions' => 'City.departament_id=Departament.id')),'order' => 'City.nombre ASC',
                    'fields' => array('City.id', 'City.nombre','Departament.nombre','Departament.id','Owner.*')));
        }
    }

    function select() {
        
        APP::Import('model', 'City');
        $City = new City();
        $City->recursive = -1;
        $this->set('cities', $City->find('list', array(
                    'order' => 'nombre ASC',
                    'conditions' => array('City.departament_id' => $this->data['Owner']['departament_id']),
                    'fields' => array('City.id', 'City.nombre')
                        )
                ));
    }

}

?>
